import { isPhone, isEmail, isNormalText } from '@/utils/validation'

export function phoneValidator (rule: any, value: any, callback: any) {
  if (!value) return callback()
  if (isPhone(value)) {
    callback()
  } else {
    callback(new Error('请输入正确手机号'))
  }
}

export function emailValidator (rule: any, value: any, callback: any) {
  if (!value) return callback()
  if (isEmail(value)) {
    callback()
  } else {
    callback(new Error('请输入正确的邮箱'))
  }
}

export function emailOrPhoneValidator (rule: any, value: any, callback: any) {
  if (!value) return callback()
  if (isEmail(value) || isPhone(value)) {
    callback()
  } else {
    callback(new Error('请输入正确的邮箱或手机号'))
  }
}

const passwordRegex = /^(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{8,16}$/

export function passwordValidator (rule: any, value: any, callback: any) {
  if (!value) return callback()
  if (passwordRegex.test(value)) {
    callback()
  } else {
    callback(new Error('8-16位，大小写字母、数字或符号3种组合'))
  }
}

export function textValidator (rule: any, value: any, callback: any) {
  if (!value) return callback()
  if (isNormalText(value)) {
    callback()
  } else {
    callback(new Error('不能包含特殊 / 等特殊字符'))
  }
}
